<template>
	<view class="order-list">
		<!-- 顶部栏 -->
		<view class="order-list__tab">
			<view class="order-list__tab__item" :class="activeTab == index  ? 'active' : ''"
				v-for="(item,index) in tabList" :key="index" @click="selectTab(item, index)">
				<view>{{ item.label }}</view>
				<view v-show="activeTab === index" class="order-list__tab__item__line"></view>
			</view>
		</view>

		<!-- 没有订单 -->
		<view class="order-list__nodata" v-if="listData.length === 0 && showFlag">
			<image class="order-list__nodata__bg" mode="widthFix" :src="$imgUrl('/order/orderlist/bg-nodata.png')">
			</image>
			<view class="order-list__nodata__text">暂无相关订单</view>
			<view class="order-list__nodata__look" @click="toLook">去逛逛</view>
		</view>

		<!-- 订单列表 -->
		<view class="order-list__box">
			<view class="order-list__item" v-for="(item,index) in listData" :key="index"
				@click="toOrderDetail(item.id)">
				<view class="order-list__item__head">
					<view class="order-list__item__time">
						<view class="line"></view>
						{{ item.createTime }}
					</view>
					<view class="order-list__item__status">{{ replaceStatus(item.status) }}</view>
				</view>
				<view class="order-list__item__body">

					<view v-if="!item.showMore" class="order-list__goods" v-for="(childItem,idx) in item.hideDetails"
						:key="idx">
						<view class="order-list__goods__left">
							<image class="order-list__goods__img" mode="widthFix" :src="childItem.recPic"></image>
							<view class="order-list__goods__name">{{ childItem.name }}</view>
						</view>
						<view class="order-list__goods__info">
							<view class="price">
								<span>￥</span>{{ (item.status == 0 || item.status == 1) ? (childItem.unitPrice ? childItem.unitPrice.toFixed(2) : '') : (childItem.disparity ? childItem.disparity.toFixed(2): '')  }}
							</view>
							<view class="count">
								{{ (item.status == 0 || item.status == 1) ?  '共' + childItem.count + '件' : '共' + childItem.sortingWeight + '斤' }}
							</view>
						</view>
					</view>
					<view v-if="item.showMore" class="order-list__goods" v-for="(childItem,idx) in item.details"
						:key="idx">
						<view class="order-list__goods__left">
							<image class="order-list__goods__img" mode="widthFix" :src="childItem.recPic"></image>
							<view class="order-list__goods__name">{{ childItem.name }}</view>
						</view>
						<view class="order-list__goods__info">
							<view class="price">
								<span>￥</span>{{ (item.status == 0 || item.status == 1) ? (childItem.unitPrice ? childItem.unitPrice.toFixed(2) : '') : (childItem.disparity ? childItem.disparity.toFixed(2): '')  }}
							</view>
							<view class="count">
								{{ (item.status == 0 || item.status == 1) ?  '共' + childItem.count + '件' : '共' + childItem.sortingWeight + '斤' }}
							</view>
						</view>
					</view>
					<!-- 超出3种商品 -->
					<view v-if="item.details.length > 3" class="order-list__over" @click.stop="setShowHide(index)">
						{{ item.showMore ? '收起' : '展开'}}(共{{ item.count }}件)
						<image class="order-list__over__icon" :class="item.showMore ? 'tr2' : 'tr'" mode="widthFix"
							:src="$imgUrl('/order/createorder/icon-right.png')"></image>
					</view>
				</view>

				<!-- 金额 -->
				<view class="order-list__footer">
					<view class="order-list__footer__amout">
						<span>合计：¥</span>{{ (item.status == 0 || item.status == 1) ? (item.amount ? item.amount.toFixed(2) : '') : (item.disparity ? item.disparity.toFixed(2) : '')}}
					</view>
					<view class="order-list__footer__btn">
						<view class="order-list__footer__time">
							<!-- 支付剩余：<span>29:18:23</span> -->
						</view>
						<view class="order-list__footer__btns">
							<view v-if="item.status == 0" class="btn color1">取消订单</view>
							<view v-if="item.status == 0" class="btn color2" @click.stop="toPay(item.code)">立即支付</view>
							<!-- <view v-if="item.status == 1 || item.status == 2" class="btn color1">申请退款</view> -->
							<!-- <view v-if="item.status == 3" class="btn color1">售后详情</view> -->
							<view
								v-if="item.status == 2 || item.status == 4 || item.status == 5 || item.status == 6 || item.status == 7"
								class="btn color2" @click.stop="confirmOrder(item.id)">确认收货</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabList: [{
					label: '全部订单',
					value: ''
				}, {
					label: '待付款',
					value: 0
				}, {
					label: '待发货',
					value: 1
				}, {
					label: '待收货',
					value: 2
				}, {
					label: '售后/退款',
					value: 3
				}],
				activeTab: 0,
				loadMore: true,
				pageNum: 1,
				pageSize: 20,
				listData: [],
				showFlag: false
			}
		},
		onLoad(options) {
			this.activeTab = options.status ? options.status : 0
		},
		onShow() {
			this.refresh()
		},
		// 下拉刷新
		onPullDownRefresh() {
			uni.showNavigationBarLoading()
			this.refresh()
			uni.hideNavigationBarLoading()
			uni.stopPullDownRefresh()
		},
		// 上拉加载更多
		onReachBottom() {
			if (this.loadMore) {
				this.pageNum = this.pageNum + 1
				this.getList()
			}
		},
		methods: {
			// 选择订单状态
			selectTab(item, index) {
				this.showFlag = false
				this.activeTab = index
				this.refresh()
			},
			toLook() {
				uni.switchTab({
					url: '/pages/type/index'
				})
			},
			// 获取
			refresh() {
				this.loadMore = true
				this.listData = []
				this.pageNum = 1
				this.getList()
			},
			// 获取精选推荐列表
			async getList() {
				const data = await this.$apis.order.getOrderList({
					pageNum: this.pageNum,
					pageSize: this.pageSize,
					status: this.tabList[this.activeTab].value
				})
				let list = data.rows
				for (let i = 0; i < list.length; i++) {
					let arr = list[i].details.slice(0, 3)
					this.$set(list[i], 'hideDetails', arr)
					this.$set(list[i], 'showMore', false)
				}
				this.listData = [...this.listData, ...list]
				this.showFlag = true
				if (this.listData.length >= data.total) {
					this.loadMore = false
				}
			},
			replaceStatus(status) {
				switch (status) { // 状态 0 待支付 1 待发货 2 待收货 3 售后
					case '0':
						return '待付款'
						break;
					case '1':
						return '待发货'
						break;
					case '2':
						return '待收货'
						break;
					case '3':
						return '售后/退款'
						break;
					default:
						break;
				}
			},
			setShowHide(index) {
				this.$set(this.listData[index], 'showMore', !this.listData[index].showMore)
			},
			async toPay(code) {
				uni.showLoading({
					mask: true
				})
				// 创建成功
				const res = await this.$apis.order.getWxpay({
					code,
					payType: 0,

				})
				uni.requestPayment({
					provider: "wxpay",
					nonceStr: res.data.nonceStr,
					package: res.data.package,
					timeStamp: res.data.timeStamp,
					paySign: res.data.sign,
					signType: res.data.signType,
					success(res) {
						uni.hideLoading()
						uni.navigateTo({
							url: '/pages/order/paySuccess/index'
						})
					},
					fail(e) {
						console.log(e);
						uni.hideLoading()
					}
				})
			},
			// 确认收货
			async confirmOrder(orderId) {
				uni.showLoading({
					mask: true
				})
				const res = await this.$apis.order.confirmOrder({
					orderId
				})
				uni.hideLoading()
				this.refresh()
			},
			toOrderDetail(id) {
				uni.navigateTo({
					url: '/pages/order/orderDetail/index?id=' + id
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.order-list {
		width: 100vw;
		min-height: 100vh;
		background: #F6F6F6;
		padding-top: 88rpx;

		.order-list__tab {
			width: calc(100% - 72rpx);
			padding: 0 36rpx;
			height: 88rpx;
			background-color: #fff;
			position: fixed;
			top: 0;
			left: 0;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.order-list__tab__item {
				height: 88rpx;
				position: relative;
				font-size: 28rpx;
				color: #333333;
				display: flex;
				align-items: center;
				justify-content: center;

				&.active {
					color: #2FCC71;
				}

				.order-list__tab__item__line {
					width: 48rpx;
					height: 4rpx;
					background: #2FCC71;
					border-radius: 3rpx;
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
				}
			}
		}

		.order-list__nodata {
			display: flex;
			flex-direction: column;
			align-items: center;

			.order-list__nodata__bg {
				width: 266rpx;
				height: 184rpx;
				margin-top: 248rpx;
			}

			.order-list__nodata__text {
				font-size: 30rpx;
				color: #333333;
				line-height: 42rpx;
				text-align: center;
				margin-top: 32rpx;
			}

			.order-list__nodata__look {
				width: 454rpx;
				height: 74rpx;
				background: #FB4041;
				border-radius: 40rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 30rpx;
				font-weight: bold;
				color: #FFFFFF;
				margin-top: 52rpx;
			}
		}

		.order-list__item {
			width: calc(710rpx - 48rpx);
			padding: 0 24rpx;
			background: #FFFFFF;
			border-radius: 16rpx;
			margin: 0 auto;
			margin-top: 20rpx;
		}

		.order-list__item__head {
			height: 88rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: 1rpx solid #E2E2E2;

			.order-list__item__time {
				display: flex;
				align-items: center;
				font-size: 28rpx;
				color: #666666;

				.line {
					width: 6rpx;
					height: 40rpx;
					background: #2FCC71;
					border-radius: 4rpx;
					margin-right: 16rpx;
				}
			}

			.order-list__item__status {
				font-size: 28rpx;
				color: #333333;
			}
		}

		.order-list__goods {
			margin-top: 24rpx;
			display: flex;
			justify-content: space-between;

			.order-list__goods__left {
				display: flex;
			}

			.order-list__goods__img {
				width: 148rpx;
				height: 148rpx;
				background: #EEEEEE;
				border-radius: 8rpx;
				margin-right: 16rpx;
			}

			.order-list__goods__name {
				width: 331rpx;
				height: 80rpx;
				font-size: 28rpx;
				color: #333333;
				line-height: 40rpx;
			}

			.order-list__goods__info {
				display: flex;
				flex-direction: column;
				align-items: flex-end;

				.price {
					font-size: 28rpx;
					font-weight: bold;
					color: #333333;
					margin-bottom: 18rpx;

					span {
						font-size: 22rpx;
					}
				}

				.count {
					font-size: 22rpx;
					color: #999999;
					line-height: 30rpx;
				}
			}
		}

		.order-list__box {
			padding-bottom: 100rpx;
		}

		.order-list__over {
			margin-top: 34rpx;
			margin-bottom: 34rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 28rpx;
			color: #999999;

			.order-list__over__icon {
				width: 44rpx;
				height: 44rpx;
				margin-left: 16rpx;

				&.tr {
					transform: rotate(90deg);
				}

				&.tr2 {
					transform: rotate(-90deg);
				}
			}
		}

		.order-list__footer__amout {
			padding-top: 24rpx;
			padding-bottom: 24rpx;
			border-top: 1rpx solid #E2E2E2;
			border-bottom: 1rpx solid #E2E2E2;
			display: flex;
			align-items: center;
			justify-content: flex-end;
			font-size: 28rpx;
			font-weight: bold;
			color: #333333;

			span {
				font-size: 22rpx;
			}
		}

		.order-list__footer__btn {
			height: 112rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.order-list__footer__time {
			font-size: 24rpx;
			font-weight: bold;
			color: #666666;

			span {
				color: #FB4041;
			}
		}

		.order-list__footer__btns {
			display: flex;
			align-items: center;

			.btn {
				width: 160rpx;
				height: 64rpx;
				border-radius: 32rpx;
				margin-left: 24rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 28rpx;

				&.color1 {
					background: #FFFFFF;
					border: 1rpx solid #CCCCCC;
					color: #666666;
				}

				&.color2 {
					background: #2FCC71;
					color: #FFFFFF;
				}
			}
		}
	}
</style>